package fr.minuskube.inv.content;

import fr.minuskube.inv.ClickableItem;

import java.util.Arrays;

public interface Pagination {
  ClickableItem[] getPageItems();
  
  int getPage();
  
  Pagination page(int paramInt);
  
  boolean isFirst();
  
  boolean isLast();
  
  Pagination first();
  
  Pagination previous();
  
  Pagination next();
  
  Pagination last();
  
  Pagination addToIterator(SlotIterator paramSlotIterator);
  
  Pagination setItems(ClickableItem... paramVarArgs);
  
  Pagination setItemsPerPage(int paramInt);
  
  public static class Impl implements Pagination {
    private int currentPage;
    
    private ClickableItem[] items = new ClickableItem[0];
    
    private int itemsPerPage = 5;
    
    public ClickableItem[] getPageItems() {
      return Arrays.<ClickableItem>copyOfRange(this.items, this.currentPage * this.itemsPerPage, (this.currentPage + 1) * this.itemsPerPage);
    }
    
    public int getPage() {
      return this.currentPage;
    }
    
    public Pagination page(int page) {
      this.currentPage = page;
      return this;
    }
    
    public boolean isFirst() {
      return (this.currentPage == 0);
    }
    
    public boolean isLast() {
      int pageCount = (int)Math.ceil(this.items.length / this.itemsPerPage);
      return (this.currentPage >= pageCount - 1);
    }
    
    public Pagination first() {
      this.currentPage = 0;
      return this;
    }
    
    public Pagination previous() {
      if (!isFirst())
        this.currentPage--; 
      return this;
    }
    
    public Pagination next() {
      if (!isLast())
        this.currentPage++; 
      return this;
    }
    
    public Pagination last() {
      this.currentPage = this.items.length / this.itemsPerPage;
      return this;
    }
    
    public Pagination addToIterator(SlotIterator iterator) {
      for (ClickableItem item : getPageItems()) {
        iterator.next().set(item);
        if (iterator.ended())
          break; 
      } 
      return this;
    }
    
    public Pagination setItems(ClickableItem... items) {
      this.items = items;
      return this;
    }
    
    public Pagination setItemsPerPage(int itemsPerPage) {
      this.itemsPerPage = itemsPerPage;
      return this;
    }
  }
}
